package com.codejiwei.log;

import com.ctc.wstx.util.DataUtil;
import org.apache.commons.lang3.time.DateParser;
import org.apache.commons.lang3.time.FastDateFormat;

import java.text.ParseException;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Author: jiwei01
 * Date: 2022/10/30 02:02
 * Package: com.codejiwei.log
 * Description:
 */
public class Parse {
    private static final DateParser DATE_PARSER = FastDateFormat.getInstance("yyyy-MM-dd HH:mm:ss", Locale.US);
    public static void main(String[] args) {

        String line = "NOTICE: 10-30 01:58:50 napistat * 0 [logid=3530696852 optime=1667066330.695 client_ip=183.225.206.73 local_ip=172.29.236.203 product=homework subsys=stat module=napistat api_module=stat api_action=addnoticev1 host=n2.zuoyebang.cc cookie= refer= uri=%2Fnapi%2Fstat%2Faddnoticev1 request_param=%7B%22queryType%22%3A%225%22%2C%22setUid%22%3A%22true%22%2C%22isStudentMode%22%3A%220%22%2C%22XSJZMSxiaodi%22%3A%22%22%2C%22sid%22%3A%22018bced1dad23f38214bfea2e52039d83732118812061178%22%2C%22uid%22%3A%22%22%2C%22tid%22%3A%229253805fca2acb9cc48514592f643b89%22%2C%22url%22%3A%22https%3A%5C%2F%5C%2Fpaisou.zuoyebang.com%5C%2Fstatic%5C%2Fhy%5C%2Fquestion-vue%5C%2Findex-d988c9de-hycache.html%22%2C%22t%22%3A%221667066315735%22%2C%22resultindex%22%3A%2224%22%2C%22ps_to_bs%22%3A%22ps_to_bs_A%22%2C%22gradeId%22%3A%2220%22%2C%22SLDB%22%3A%220%22%2C%22vcname%22%3A%2213.42.0%22%2C%22subId%22%3A%225%22%2C%22isActived%22%3A%22false%22%2C%22setCity%22%3A%22true%22%2C%22vc%22%3A%221380%22%2C%22isteachermode%22%3A%220%22%2C%22reportTime%22%3A%221667066330798%22%2C%22stuparmode%22%3A%221%22%2C%22pageInit%22%3A%22true%22%2C%22singlequerysource%22%3A%22%22%2C%22page%22%3A%22%5C%2Fstatic%5C%2Fhy%5C%2Fquestion-vue%5C%2Findex-d988c9de-hycache%22%2C%22city%22%3A%22%E6%9B%B2%E9%9D%96%E5%B8%82%22%2C%22setData%22%3A%22true%22%2C%22cuid%22%3A%22E0AA090A231909F85C5C1104C1B6983B%7C0%22%2C%22numc%22%3A%22%22%2C%22screen%22%3A%22360_760%22%2C%22newAutoPage%22%3A%220%22%2C%22searchpageType%22%3A%220%22%2C%22serveSet%22%3A%22%7B%5C%22querytype%5C%22%3A5%2C%5C%22resultType%5C%22%3A0%2C%5C%22searchTag%5C%22%3A0%2C%5C%22singlequerysource%5C%22%3A%5C%22%5C%22%2C%5C%22wholeQueryType%5C%22%3A5%7D%22%2C%22moreresultindex%22%3A%228-0%22%2C%22os%22%3A%22android%22%2C%22type%22%3A%22custom%22%2C%22courseId%22%3A%225%22%2C%22name%22%3A%22PS_C6_0_3%22%2C%22isHighQuality%22%3A%220%22%7D cost=0.000 ua=Mozilla%2F5.0%20(Linux%3B%20Android%208.1.0%3B%20vivo%20Y85%20Build%2FOPM1.171019.011%3B%20wv)%20AppleWebKit%2F537.36%20(KHTML%2C%20like%20Gecko)%20Version%2F4.0%20Chrome%2F68.0.3440.91%20Mobile%20Safari%2F537.36%20homework_vc%2F1380%20homework_vcname%2F13.42.0%20homework_cuid%2FE0AA090A231909F85C5C1104C1B6983B%7C0%20homework_token%2F1_XPXQH3c5HRPtFHkSwi3sCCURmT25QfxM%20homework_channel%2Fvivo%20pad%2F0%20HyAppName%2Fhomework%20HySdkVer%2F0.7.10-rc9%20SubAppId%2Fhomework%20zyb_jsBridge%2F1%20jsBridge_jsInterface%2F1 status=200]";

        Pattern pattern = null;
        Matcher matcher = null;
        // 取日志的时间
        Date date = null;
        //SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        pattern = Pattern.compile("(NOTICE: (\\d\\d\\-\\d\\d \\d\\d:\\d\\d:\\d\\d).*)");
        matcher = pattern.matcher(line);
        if (matcher.find()) {
            try{
                String monthDate = matcher.group(2);
                Calendar now = Calendar.getInstance();
                date = DATE_PARSER.parse(now.get(Calendar.YEAR) + "-" + monthDate);
                //日志的获取当前年之后的日期 大于 当前日期 则需要获取上一年日期
                if(date.compareTo(new Date()) >= 0) {
                    now.add(Calendar.YEAR, -1);
                    date = DATE_PARSER.parse(now.get(Calendar.YEAR) + "-" + monthDate);
                }
            } catch (ParseException e) {
                e.printStackTrace();
            }
        }
        System.out.println(date);


        pattern = Pattern.compile("([^=\\[]*)=([^\\s\\]]*)");
        matcher = pattern.matcher(matcher.group(1));

        while (matcher.find()) {
            String k = matcher.group(1).trim();
            String v = matcher.group(2).trim();
            System.out.println(k);
            System.out.println(v);
            System.out.println("======================");
        }

        String k = "";
        String v = "";

//        System.out.println(matcher.find());
//        System.out.println(matcher.group(1));
//        System.out.println(matcher.group(2));


    }
}